<template>
    <view class="wai">
        <image src="@/static/logo.jpg" mode=""></image>
        <view @click="check">
            <button :class="{ 'disabled-class': !ischak }" :disabled="!ischak" class="dlbutton" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">微信登录</button>
        </view>
        <!-- <button @click="logins">账号密码登录</button> -->

        <view class="foter">
            <checkbox-group>
                <checkbox @click="btng" :checked="ischak" class="red round" value="cb" style="transform: scale(0.8)" />
            </checkbox-group>
            <view class="btm1">
                请阅读并同意
                <text @click="btnpRichtext('/pages/sub/privacy')" class="color">《用户协议》</text>
                与
                <text @click="btnpRichtext('/pages/sub/privacy1')" class="color">《隐私协议》</text>
            </view>
        </view>
    </view>
</template>

<script setup>
import { ref } from 'vue';
import { loginIndex, getSessionId, wechatLogin, bindPhone } from '@/api/index.js';
	import { onLoad, onShow} from '@dcloudio/uni-app';
import { utils } from '@/utils/utils.js';

const invite_code = ref(uni.getStorageSync('invite_code'))
const ischak = ref(false); //用户协议勾选框
// 点击切换选择框状态
onLoad(() => {
});
const btng = () => {
    ischak.value = !ischak.value;
};
//点击提醒勾选协议
const check = () => {
    if (!ischak.value) {
        utils('请勾选用户协议');
    }
};
// 账号密码登录
const logins = async () => {
    const account = 'admin';
    const password = '123456';
    // 通常登录请求需要一个包含用户名和密码的对象
    const credentials = { account: account, password: password };
    const res = await loginIndex(credentials);
    if (res.code == 1) {
        utils('登录成功');
        uni.setStorageSync('token', res.data.userinfo.token);
       uni.navigateBack({
		   delta:1
       })
    }
    console.log(res);
};
// 微信登录
const onGetPhoneNumber = async (e) => {
    console.log(e);
    if (e.detail.errMsg === 'getPhoneNumber:ok') {
        // 用户已经授权
        const iv = e.detail.iv;
        const encryptedData = e.detail.encryptedData;
        uni.login({
            provider: 'weixin',
            onlyAuthorize: true,
            success: async function (loginRes) {
                console.log('获取登录凭证成功', loginRes);
                // 获取SesionId
                const getSessid = await getSessionId(loginRes.code);
                console.log('获取getSessid', getSessid);
                if (getSessid.code == 1) {
                    // 微信登录
                    const ret = await wechatLogin(getSessid.data.session_id,invite_code.value);
                    console.log('登录成功', ret);
                    if (ret.code == 1) {
                        if (ret.data.userinfo.mobile.length != 11) {
                            console.log('未绑定手机号');
                            // 未绑定手机号,去绑定手机号
                            const res = await bindPhone(getSessid.data.session_id, iv, encryptedData, ret.data.userinfo.token);
                            if (res.code == 1) {
                                uni.setStorageSync('token', ret.data.userinfo.token);
                                uni.navigateBack({
									delta:1
                                })
                                utils('登录成功');
                            } else {
                                utils(ret.msg);
                            }
                        } else {
                            // 已绑定手机号
                            console.log('已绑定手机号');
                            uni.setStorageSync('token', ret.data.userinfo.token);
                           uni.navigateBack({
							   delta:1
						   })
                            utils('登录成功');
                        }
                    }
                } else {
                    utils(`登录失败，${getSessid.msg}`);
                }
            },
            fail: function (err) {
                console.error('获取登录凭证失败', err);
            }
        });
    } else {
        // 用户未授权
        console.error('用户未授权获取手机号');
    }
};
// 跳转用户协议和隐私政策
const btnpRichtext = (url) => {
    uni.navigateTo({
        url: url
    });
};
</script>

<style lang="scss" scoped>
.wai {
    .foter {
        position: fixed;
        bottom: 30rpx;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        .btm1 {
            .color {
                color: rgb(118, 193, 66);
            }
        }
    }
    text-align: center;
}
.disabled-class {
    z-index: -1;
}
// 微信登录按钮样式
.dlbutton {
    margin-top: 40upx !important;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #ffffff !important;
    font-size: 30rpx;
    white-space: nowrap;
    overflow: hidden;
    width: 601rpx;
    height: 100rpx;
    background: linear-gradient(to right, rgb(118, 193, 66), rgb(118, 193, 66));
    box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(164, 217, 228, 0.4);
    border-radius: 2.5rem;
}
</style>
